/**
* 作者: lby
* 日期: 2024-11-13 20:27:09
* 备注: 用户未婚子嗣表
*/
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBMarryInfo;
import com.motu.monstercity.protocol.Common.PBUserChildUnmarried;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.ChildManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class UserChildUnmarried extends UserChildUnmarriedModel {
	public UserChildUnmarried() {
		super();
	}

	public void reset() {
		if (getMarryTime() > 0 && !isMarryTime()) {// 之前有发布征婚，现在时间过期了
			UserInfo userInfo = GameUser.getUserInfo(getUserId());
			ChildManager.cancelMarryInfo(userInfo, this, null, null);// 删除servermarry数据，和 榜单数据匹配池子
			putMarryTime(0);
			putMarryState((byte) 0);
			putUserIdTarget(0);
			update();
		}
	}

	// 生成小孩的数据, index 第几个小孩,双胞胎的情况,0 第一个,1双胞胎的第二个
	public UserChildUnmarried(long userId, UserChildCls userChildCls, int index) {
		super();
		putUserId(userId);
		putName(index == 1 ? userChildCls.getSisterName() : userChildCls.getName());
		putQuality(userChildCls.getQuality());
		putCareer(userChildCls.getCareer());
		putPartner(userChildCls.getPartner());
		putSecretary(userChildCls.getSecretary());
		putAppearance(userChildCls.getAppearance());
		putSex(index == 1 ? userChildCls.getSisterSex() : userChildCls.getSex());
		putEarnAdd(userChildCls.checkIsTwins() ? userChildCls.getAddition() / 2 : userChildCls.getAddition());
		putCharacterId(userChildCls.getCharacterId());
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public JsonObject insert() {
		// 如果超过上限则会删除最早的那条
		long count = Future.await(RedisTools.getHashLen(getListKey()));
		if (count >= AllParam.CHILD_UNMARRIED_MAX) {
			String orderListKey = EntityManager.getOrderListKey(getListKey());
			String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
			long id = Long.parseLong(delRecord);// dbid
			UserChildUnmarried userChildUnmarried = ChildManager.getUserChildUnmarried(getUserId(), id);
			if (userChildUnmarried != null) {
				userChildUnmarried.delete();
			}
		}
		return super.insert();
	}

	// 是否在征婚有效时间内
	public boolean isMarryTime() {
		if (!isMarryServer() && !isMarryUser()) {
			return false;
		}
		if (getMarryTime() > 0 && TimeUtils.getTimeDiff(getMarryTime()) < AllParam.CHILD_MARRY_ACTIVE_TIME) {
			return true;
		} else {
			return false;
		}
	}

	// 是否男性
	public boolean isMale() {
		if (getSex() == 1) {
			return true;
		} else {
			return false;
		}
	}

	// 是否全服联姻
	public boolean isMarryServer() {
		return getMarryState() == 1;
	}

	// 是否指定玩家联姻
	public boolean isMarryUser() {
		return getMarryState() == 2;
	}

	public PBUserChildUnmarried.Builder takeInitBuilder() {
		PBUserChildUnmarried.Builder sb = super.takeInitBuilder();
		addMarryInfo(sb);// 匹配到的征婚信息
		return sb;
	}

	public PBUserChildUnmarried.Builder takeBuilder() {
		PBUserChildUnmarried.Builder sb = super.takeBuilder();
		addMarryInfo(sb);// 匹配到的征婚信息
		return sb;
	}

	public void addMarryInfo(PBUserChildUnmarried.Builder sb) {
		UserInfo userInfo = GameUser.getUserInfo(getUserId());
		int serverId = userInfo.getServerId();
		JsonArray jsonArray = getMatchInfoArray();// 匹配到玩家列表
		for (int i=0;i<jsonArray.size();i++) {
			long dbid = jsonArray.getLong(i);// 对方的子嗣的id
			ServerMarryInfo serverMarryInfo = ChildManager.getServerMarryInfo(serverId, dbid);
			if (serverMarryInfo != null) {
				UserChildUnmarried userChildUnmarriedTarget = ChildManager.getUserChildUnmarried(serverMarryInfo.getUserId(), dbid);
				UserInfo userInfoTarget = GameUser.getUserInfo(serverMarryInfo.getUserId());
				PBMarryInfo.Builder builder = ChildManager.takePBMarryInfo(userInfoTarget, userChildUnmarriedTarget);
				builder.setEarnAddLimit(serverMarryInfo.getMinEarnAdd());// 对方设置的条件
				sb.putMatchInfoList(dbid, builder.build());
			}
		}

		JsonArray jsonArray2 = getMatchInfoRobotArray();// 机器人列表
		for (int i=0;i<jsonArray2.size();i++) {
			long dbid = jsonArray2.getLong(i);
			PBMarryInfo.Builder builder = PBMarryInfo.newBuilder();
			UserMarryRobot userMarryRobot = ChildManager.getUserMarryRobot(getId(), dbid);
			if (userMarryRobot != null) {
				builder.setDbid(userMarryRobot.getId());
				builder.setEarnAdd(userMarryRobot.getEarnAdd());
			}
			sb.putMatchInfoRobotList(dbid, builder.build());
		}
	}
}
